//      main_template.cc
//      template for new programs
//      
//      Copyright 2012 tqlong <tqlong@espada>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.

/* Compiling
gcc main_template.cc helper/stl_helper.cc helper/rand_helper.cc helper/dna_processing.cc -l boost_program_options -o main.template

./main.template --help
 */

#include <iostream>
#include <boost/program_options.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> 

#include <armadillo>

#include "helper/stl_helper.h"
#include "helper/ublas_helper.h"
#include "helper/ml_data.h"

namespace po = boost::program_options;
po::options_description desc("Allowed options");
po::variables_map vm;

void process_options(int argc, char** argv);
void usage(int exitcode = 0);

void read_data();

#define NOW boost::posix_time::microsec_clock::local_time()
#define TIME_FROM_START (NOW - __start_time)
boost::posix_time::ptime __start_time = NOW;

int main(int argc, char** argv)
{
  //MSG("Program title ... ");

  process_options(argc, argv);

  if (vm.count("help"))
    usage();
  else if (vm.count("read"))
    read_data();
  else
    usage();

  MSG("Duration = " << TIME_FROM_START);
}

void process_options(int argc, char** argv)
{
  desc.add_options()
    ("help", "produce this usage")
    ("input", po::value<std::string>(), "input file")
    ("output", po::value<std::string>(), "output file")
    ("debug", po::value<std::string>(), "debug file")
    ("verbose", "print debug information")

    // GENERAL param

    // OPTIMIZATION param
    ("optim_iter", po::value<int>(), "optim: maximum number of iterations")
    ("optim_tol", po::value<double>(), "optim: tolerance")
    ("optim_mem", po::value<int>(), "optim: L-BFGS memory")
    ("optim_eta", po::value<double>(), "optim: fixed step size")

    // MODULE param

    //JOB type
    ("read", "read data file\n--input [--output]")
    ("test", "test UBLAS")
    ;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);
}

void usage(int exitcode)
{
  std::cout << desc << std::endl;
  exit(exitcode);
}

void read_data()
{  
  std::string inputFile = vm["input"].as<std::string>();
  typedef DataTable<sparse_vec_t, sparse_matrix_t> Data;
  //typedef DataTable<vec_t, matrix_t> Data;

  Data D;
  DataReader::read_libsvm(inputFile, D, true); // add a bias term
  MSG("sz(D)=" << sz(D) << " dim(D)=" << D.dim());
}
